﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forget.HRBLL.Model;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Forget.Data;

namespace Forget.HRBLL.Logical
{
    public class UserLogical
    {
        /// <summary>
        /// HR登录逻辑
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static User Login(string userName, string password)
        {
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
                return User.Login(new User() { UserName = userName, PassWord = Common.GetMD5(password) });
            else
                throw new Exception("username and password can not empty.");
        }

        public static bool CreateUser(string username, string numberID, string email, string phone, string name)
        {
            if (!string.IsNullOrEmpty(numberID) && !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(name))
            {
                int length = 6;
                int.TryParse(ConfigurationManager.AppSettings["PasswordLength"], out length);
                string password = Common.GetRandomChars(length);
                User user = new User() { NumberID = numberID, UserName = username, Email = email, Name = name, PassWord = Common.GetMD5(password), Phone = phone };
                if (!string.IsNullOrEmpty(phone))
                    user.Phone = phone;
                if (User.AddUserInfo(user))
                {
                    Mail.SendMain(user.Email, "Your password is", password);
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

        public static bool ModifyUserBaseInfo(string numberID, string email, string phone,string name)
        {
            if (!string.IsNullOrEmpty(numberID))                
                return User.EditUserInfo(new User() { NumberID = numberID, Email = email, Phone = phone, Name=name });
            
            return false;
        }

        public static bool ModifyPassword(string numberID,string username,string password)
        {
            if (!string.IsNullOrEmpty(numberID) && !string.IsNullOrEmpty(password))
            {
                int length = 6;
                int.TryParse(ConfigurationManager.AppSettings["PasswordLength"], out length);
                if (password.Length < length)
                    throw new ArgumentException("It is short that the password has length.");
                return User.ResetPassword(new User() { NumberID = numberID, UserName=username, PassWord = Common.GetMD5(password) });
            }
            return false;
        }

        public static User GetUserInfo_NumberID(string numberID)
        {
            if (!string.IsNullOrEmpty(numberID))
                return User.GetUserInfo_NumberID(new User() { NumberID = numberID });
            return null;
        }
    }   
}

namespace Forget.HRBLL.Model
{
    public partial class User
    {
        private static readonly string RESETPASSWORD = "HR_UserPasswordReset";
        private static readonly string USERLOGIN = "HR_UserLogin";
        private static readonly string SELUSERINFO_NUMBERID = "HR_SelUserinfo_NumberID";
        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="numberID">用户编号</param>
        /// <param name="password">密码明文</param>
        /// <returns></returns>
        public static bool ResetPassword(User user)
        {
            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(user);
            try
            {
                if (DataAccess.ExecuteNonQuery(CommandType.StoredProcedure, RESETPASSWORD, parms.ToArray()) > 0)
                    return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.ResetPass", ex.Message, ex.StackTrace);
            }

            return false;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户登录名</param>
        /// <param name="passWord">用户登录密码</param>
        /// <returns>如果登录成功返回用户基本信息，否则返回空</returns>
        public static User Login(User user)
        {
            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(user);
            List<User> users = null;
            try
            {
                DataTable table = DataAccess.ExecuteDataTable(CommandType.StoredProcedure, USERLOGIN, parms.ToArray());
                users = Convert<User>.DataConvertToObject(table);
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.Login", ex.Message, ex.StackTrace);
            }
            if (users != null && users.Count > 0)
                return users[0];
            return null;
        }


        public static User GetUserInfo_NumberID(User user)
        {
            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(user);
            List<User> users = null;
            try
            {
                DataTable table = DataAccess.ExecuteDataTable(CommandType.StoredProcedure, SELUSERINFO_NUMBERID, parms.ToArray());
                users = Convert<User>.DataConvertToObject(table);
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.UserNuberID", ex.Message, ex.StackTrace);
            }
            if (users != null && users.Count > 0)
                return users[0];
            return null;
        }
    }
}